package com.naiterui.ehp.bs.doctor.controller.inner;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import io.swagger.annotations.Api;
import org.apache.commons.codec.Charsets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.ehp.bp.bo.doctor.DoctorAuditParamBO;
import com.naiterui.ehp.bp.bo.doctor.RecordParamAuditBO;
import com.naiterui.ehp.bp.support.exception.BaseExceptionCodes;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.service.IAuditService;

@RestController
@RequestMapping("ad/inner/audit")
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.INNER_API + SwaggerTag.AUTH_RECORD})
public class AdInnerAuditController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdInnerAuditController.class);

    @Autowired
    private IAuditService auditService;

    /**
     * 医生认证审核
     *
     * @param audit param
     *
     * @return
     *
     * @throws BusinessException
     */
    @PostMapping("auth/save")
    public ResponseVO<Void> authAudit(@RequestHeader("operator") String operator, @RequestBody DoctorAuditParamBO audit) throws BusinessException {
        try {
            operator = URLDecoder.decode(operator, Charsets.UTF_8.name());
        } catch (UnsupportedEncodingException ignored) {
        }

        LOGGER.info("操作人:{} 医生认证审核状态更新 {}", operator, audit);

        if (audit.validate()) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        auditService.updateAuthAudit(operator, audit);

        return PublicService.returnResponseVO(BaseExceptionCodes.SUCCESS);
    }

    /**
     * 医生备案审核
     *
     * @param audit param
     *
     * @return
     *
     * @throws BusinessException
     */
    @PostMapping("record/save")
    public ResponseVO<Void> recordAudit(@RequestHeader("operator") String operator, @RequestBody RecordParamAuditBO audit) throws BusinessException {
        try {
            operator = URLDecoder.decode(operator, Charsets.UTF_8.name());
        } catch (UnsupportedEncodingException ignored) {
        }
        LOGGER.info("操作人:{} 医生备案审核保存 {}", operator, audit);
        if (audit.validate()) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        auditService.saveRecordInfo(operator, audit);

        return PublicService.returnResponseVO(BaseExceptionCodes.SUCCESS);
    }

}
